package com.vast.admin.utils;

import com.vast.admin.entity.RepaymentPlanWord;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: 先息后本还款方式工具类型</p>
 */
public class BIAPPUtils {

    /**
     * 精度
     */
    private static final int PRECISION = 3;

    /**
     * 计算方式 四舍五入
     */
    public static final RoundingMode SAVEROUNDINGMODE = RoundingMode.HALF_UP;

    /**
     * 运算上下文（设置精度、舍入规则）
     */
    private static final MathContext MATHCONTEXT = new MathContext(PRECISION, SAVEROUNDINGMODE);

    /**
     * <p>Description: 月还款利息  贷款本金×月利率 </p>
     * @param loan 贷款本金
     * @param monthlyInterestRate 月利率
     * @return
     */
    private static BigDecimal monthlyInterest(BigDecimal loan, BigDecimal monthlyInterestRate){
        return loan.multiply(monthlyInterestRate, MATHCONTEXT);
    }
    /**
     * <p>Description: 还款总利息  贷款本金×月利率×期数</p>
     * @param loan 贷款本金
     * @param monthlyInterestRate 月利率
     * @param number 期数
     * @return
     */
    private static BigDecimal interest(BigDecimal loan, BigDecimal monthlyInterestRate, int number){
        return loan.multiply(monthlyInterestRate).multiply(new BigDecimal(number), MATHCONTEXT);
    }
    /**
     * <p>Description: 月还款额</p>
     * @param loan 贷款本金
     * @param monthlyInterestRate 月利率
     * @param amount 期数
     * @param curNumber 当前期数
     * @return
     */
    private static BigDecimal monthlyRepayment(BigDecimal loan, BigDecimal monthlyInterestRate, int amount, int curNumber){
        BigDecimal monthlyInterest = monthlyInterest(loan, monthlyInterestRate);
        if (amount == curNumber) {
            return monthlyInterest.add(loan, MATHCONTEXT);//最后月还款额
        } else {
            return monthlyInterest;
        }
    }

    /**
     * 计算先息后本还款计划
     * @param invest
     * @param monthRate
     * @param totalMonth
     * @param dateStr
     * @return
     */
    public static List<RepaymentPlanWord> cal(double invest, double monthRate, int totalMonth, List<String> dateStr) {
        BigDecimal investBigDecimal = new BigDecimal(invest);
        BigDecimal monthRateBigDecimal = new BigDecimal(monthRate);
        BigDecimal monthlyInterestBigDecimal = monthlyInterest(investBigDecimal, monthRateBigDecimal);
        double monthlyInterest = monthlyInterestBigDecimal.doubleValue();
        List<RepaymentPlanWord> repaymentPlanWords = new ArrayList<>(totalMonth);
        for (int i = 1; i <= totalMonth; i ++) {
            RepaymentPlanWord repaymentPlanWord = new RepaymentPlanWord();
            repaymentPlanWord.setRepaymentIndex(String.valueOf(i));
            repaymentPlanWord.setRepaymentDate(dateStr.get(i - 1));
            if (i != totalMonth) {
                repaymentPlanWord.setTotal(String.valueOf(monthlyInterest));
                repaymentPlanWord.setPrincipal("0");
            } else {
                repaymentPlanWord.setTotal(String.valueOf(investBigDecimal.add(monthlyInterestBigDecimal).doubleValue()));
                repaymentPlanWord.setPrincipal(String.valueOf(invest));
            }
            repaymentPlanWord.setInterest(String.valueOf(monthlyInterest));
            repaymentPlanWords.add(repaymentPlanWord);
        }
        return repaymentPlanWords;
    }

    public static void main(String[] args) {
        System.out.println(BIAPPUtils.cal(300000, 0.025, 6, new ArrayList<String>(){{
            add("1");
            add("2");
            add("3");
            add("4");
            add("5");
            add("6");
        }}));

    }


}
